<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深入理解Java ByteBuffer：从基础到高级应用</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 8px;
            overflow-x: auto;
            position: relative;
        }
        
        .code-block::before {
            content: 'Java';
            position: absolute;
            top: 0;
            right: 0;
            padding: 4px 12px;
            background: rgba(255, 255, 255, 0.1);
            border-radius: 0 8px 0 8px;
            font-size: 12px;
            color: #888;
        }
        
        h2 {
            position: relative;
            padding-left: 20px;
        }
        
        h2::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 20px;
            border-radius: 12px;
            margin: 20px 0;
        }
        
        .concept-card {
            background: white;
            border-radius: 12px;
            padding: 24px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
            transition: all 0.3s ease;
            border: 1px solid #e5e7eb;
        }
        
        .concept-card:hover {
            transform: translateY(-4px);
            box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
        }
        
        .mermaid {
            background: white;
            padding: 20px;
            border-radius: 12px;
            margin: 20px 0;
        }
        
        .drop-cap {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin: 0.1em 0.1em 0.1em 0;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-family: 'Noto Serif SC', serif;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl font-bold mb-6 leading-tight">
                    深入理解Java ByteBuffer
                </h1>
                <p class="text-2xl mb-8 opacity-90">从基础到高级应用的完整指南</p>
                <div class="flex justify-center space-x-8 text-lg">
                    <div class="flex items-center">
                        <i class="fas fa-book-open mr-2"></i>
                        <span>全面解析</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-code mr-2"></i>
                        <span>实战示例</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-rocket mr-2"></i>
                        <span>性能优化</span>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <div class="max-w-5xl mx-auto">
            
            <!-- Introduction -->
            <div class="content-card rounded-xl shadow-xl p-8 mb-8">
                <p class="text-lg leading-relaxed text-gray-700">
                    <span class="drop-cap">在</span>Java网络编程和高性能IO处理中，ByteBuffer是一个不可或缺的核心组件。作为Java NIO（New I/O）库的基石，它彻底改变了Java进行I/O操作的方式，从传统的面向流的阻塞式I/O转变为面向缓冲区的非阻塞式I/O。然而，很多开发者对ByteBuffer的理解仅停留在表面，无法充分发挥其强大功能，甚至在使用过程中踩坑不断。
                </p>
            </div>

            <!-- ByteBuffer Architecture Diagram -->
            <div class="content-card rounded-xl shadow-xl p-8 mb-8">
                <h3 class="text-2xl font-bold mb-6 text-gray-800">
                    <i class="fas fa-sitemap mr-3 text-purple-600"></i>ByteBuffer架构概览
                </h3>
                <div class="mermaid">
                    graph TB
                        A[ByteBuffer] --> B[HeapByteBuffer]
                        A --> C[DirectByteBuffer]
                        B --> D[JVM堆内存]
                        C --> E[堆外内存]
                        
                        A --> F[核心属性]
                        F --> G[capacity]
                        F --> H[position]
                        F --> I[limit]
                        
                        A --> J[核心操作]
                        J --> K[put/get]
                        J --> L[flip/clear]
                        J --> M[compact/rewind]
                        
                        style A fill:#667eea,stroke:#fff,stroke-width:3px,color:#fff
                        style B fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                        style C fill:#f5576c,stroke:#fff,stroke-width:2px,color:#fff
                </div>
            </div>

            <!-- Section 1: ByteBuffer基础 -->
            <div class="content-card rounded-xl shadow-xl p-8 mb-8">
                <h2 class="text-3xl font-bold mb-8 text-gray-800">一、ByteBuffer基础</h2>
                
                <div class="mb-8">
                    <h3 class="text-2xl font-semibold mb-4 text-gray-700">
                        <i class="fas fa-question-circle mr-2 text-purple-600"></i>1.1 什么是ByteBuffer？
                    </h3>
                    <p class="text-lg leading-relaxed text-gray-600 mb-6">
                        ByteBuffer是Java NIO包中的一个核心类，它是Buffer抽象类的一个具体实现，专门用于操作字节数据。与传统的字节数组不同，ByteBuffer提供了一种更加灵活和高效的方式来处理字节数据，特别是在需要频繁读写操作的场景下。
                    </p>
                    
                    <div class="code-block p-6 mb-6">
                        <pre><code>import java.nio.ByteBuffer;

public class ByteBufferBasics {
    public static void main(String[] args) {
        // 创建一个容量为10字节的ByteBuffer
        ByteBuffer buffer = ByteBuffer.allocate(10);
        System.out.println("初始状态:");
        printBufferStatus(buffer);
        
        // 向缓冲区写入数据
        buffer.put((byte) 1);
        buffer.put((byte) 2);
        buffer.put((byte) 3);
        System.out.println("写入3个字节后:");
        printBufferStatus(buffer);
        
        // 切换到读模式
        buffer.flip();
        System.out.println("切换到读模式后:");
        printBufferStatus(buffer);
        
        // 读取数据
        while (buffer.hasRemaining()) {
            byte b = buffer.get();
            System.out.println("读取: " + b);
        }
        
        System.out.println("读取完所有数据后:");
        printBufferStatus(buffer);
        
        // 准备再次写入数据，清除缓冲区
        buffer.clear();
        System.out.println("清除后:");
        printBufferStatus(buffer);
    }
    
    private static void printBufferStatus(ByteBuffer buffer) {
        System.out.println("position: " + buffer.position() +
                           ", limit: " + buffer.limit() +
                           ", capacity: " + buffer.capacity());
    }
}</code></pre>
                    </div>
                    
                    <p class="text-gray-600">
                        这个例子展示了ByteBuffer的基本用法：创建、写入数据、切换到读模式、读取数据，最后清除准备重用。
                    </p>
                </div>

                <!-- Core Properties -->
                <div class="mb-8">
                    <h3 class="text-2xl font-semibold mb-6 text-gray-700">
                        <i class="fas fa-cogs mr-2 text-purple-600"></i>1.2 Buffer的核心属性
                    </h3>
                    
                    <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-6">
                        <div class="concept-card">
                            <div class="text-3xl mb-3 text-purple-600">
                                <i class="fas fa-database"></i>
                            </div>
                            <h4 class="text-xl font-bold mb-2">capacity</h4>
                            <p class="text-gray-600">缓冲区的最大容量，创建后不可改变</p>
                        </div>
                        
                        <div class="concept-card">
                            <div class="text-3xl mb-3 text-pink-600">
                                <i class="fas fa-map-marker-alt"></i>
                            </div>
                            <h4 class="text-xl font-bold mb-2">position</h4>
                            <p class="text-gray-600">当前位置，读/写操作都是从position开始，操作后position会自动前进</p>
                        </div>
                        
                        <div class="concept-card">
                            <div class="text-3xl mb-3 text-indigo-600">
                                <i class="fas fa-flag-checkered"></i>
                            </div>
                            <h4 class="text-xl font-bold mb-2">limit</h4>
                            <p class="text-gray-600">可访问数据的边界，在写模式下等于capacity，读模式下等于写入的数据量</p>
                        </div>
                    </div>
                    
                    <div class="code-block p-6">
                        <pre><code>import java.nio.ByteBuffer;

public class BufferProperties {
    public static void main(String[] args) {
        // 创建缓冲区
        ByteBuffer buffer = ByteBuffer.allocate(10);
        
        System.out.println("初始状态:");
        System.out.println("capacity: " + buffer.capacity());  // 10
        System.out.println("position: " + buffer.position());  // 0
        System.out.println("limit: " + buffer.limit());        // 10
        
        // 写入4个字节
        buffer.put((byte) 1).put((byte) 2).put((byte) 3).put((byte) 4);
        
        System.out.println("\n写入4个字节后:");
        System.out.println("capacity: " + buffer.capacity());  // 10
        System.out.println("position: " + buffer.position());  // 4
        System.out.println("limit: " + buffer.limit());        // 10
        
        // 切换到读模式
        buffer.flip();
        
        System.out.println("\n切换到读模式后:");
        System.out.println("capacity: " + buffer.capacity());  // 10
        System.out.println("position: " + buffer.position());  // 0
        System.out.println("limit: " + buffer.limit());        // 4
    }
}</code></pre>
                    </div>
                </div>

                <!-- Creation Methods -->
                <div class="mb-8">
                    <h3 class="text-2xl font-semibold mb-6 text-gray-700">
                        <i class="fas fa-plus-circle mr-2 text-purple-600"></i>1.3 ByteBuffer的创建方式
                    </h3>
                    
                    <div class="highlight-box mb-6">
                        <h4 class="text-xl font-bold mb-3">
                            <i class="fas fa-lightbulb mr-2"></i>四种创建方式
                        </h4>
                        <ul class="space-y-2">
                            <li><i class="fas fa-check mr-2"></i>allocate() - 在JVM堆上分配内存</li>
                            <li><i class="fas fa-check mr-2"></i>allocateDirect() - 分配直接内存（